Sample extension to demonstrate integration with an OAuth service.
Overview
--------
This sample demonstrates the use of OAuth to authorize against
Google's Contacts API inside of an extension. It implements a library which
may be reused generically to authorize requests to any 3-legged OAuth API.
Library
-------
The library files are:
* chrome_ex_oauth.html
* chrome_ex_oauth.js
* chrome_ex_oauthsimple.js
To use these files, place them in the root of your extension and include both
.js files in your background page in the following order:
To initialize the API, create a ChromeExOAuth object in the background page:
var oauth = ChromeExOAuth.initBackgroundPage({
'request_url' : ,
'authorize_url' : ,
'access_url' : ,
'consumer_key' : ,
'consumer_secret' : ,
'scope' : ,
'app_name' :
});
Call the authorize() function to redirect the user to the OAuth provider in
order to obtain an access token. The client library abstracts most of this
process, so all you need to do is pass a callback to the authorize() function
and a new tab will open and redirect the user. If the library already has
stored an access token for the current scope, then no tab will be opened. In
either case, the callback will be called with the resulting token and secret.
oauth.authorize(onAuthorized);
There is no need to store the token and secret, as this library already stores
these values in localStorage. Once the callback you specified is called, you
can call the sendSignedRequest function to send OAuth-signed requests to the
API. The sendSignedRequest call takes an url to fetch, a callback function,
and an optional parameter object as its arguments. The callback is passed
the response text as well as the XMLHttpRequest object which was used to
make the request as its arguments.
function callback(text, xhr) {
//...
};
function onAuthorized() {
var url = ;
var request = {
'method' : 'GET',
'parameters' : {
}
}
oauth.sendSignedRequest(url, callback, request);
};
oauth.authorize(onAuthorized);